package de.uni.bremen.estudent.lbs.ui.phone;


import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;

import de.uni.bremen.estudent.lbs.views.CompasView;


public final class CompassActivity extends Activity implements
                                                   SensorEventListener {

    CompasView mCompassView;
    Sensor mSensor;
    SensorManager mSensorManager;

    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCompassView = new CompasView(getApplicationContext());
        setContentView(mCompassView);

        // Register Sensor
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    }

    @Override
    public void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mSensor,
                                        SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        int orientation = (int) event.values[0];
        mCompassView.setDirection(orientation);
        Log.d("CompassActivity", "Orientation: " + orientation);
    }

}
